package com.itboye.ihomebank.adapter.key;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.util.AutoMarqueeTextView;
import com.itboye.ihomebank.widget.BatteryView;

import java.util.List;

/**
 * Created by Itboye-000018 on 2017/7/20.
 */
public class KeyTwoAdapter extends BaseAdapter {
    Context context;
    List<Key> list;
    public KeyTwoAdapter(Context context, List<Key> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {
        ViewHolder holder=null;
        if(arg1==null){
            arg1= LayoutInflater.from(context).inflate(R.layout.item_my_key, null);
            holder=new ViewHolder();
            holder.my_key_type=(TextView) arg1.findViewById(R.id.my_key_type);
            holder.item_my_zhuangtai=(TextView) arg1.findViewById(R.id.item_my_zhuangtai);
            holder.my_key_name=(AutoMarqueeTextView) arg1.findViewById(R.id.my_key_name);
            holder.item_my_dianliang=(TextView) arg1.findViewById(R.id.item_my_dianliang);
            holder.my_key_xianshi=(TextView) arg1.findViewById(R.id.my_key_xianshi);
            holder.batteryView= (BatteryView) arg1.findViewById(R.id.batteryView);
            holder.personIv= (ImageView) arg1.findViewById(R.id.personIv);
            holder.hasValidRent_status= (TextView) arg1.findViewById(R.id.hasValidRent_status);
            arg1.setTag(holder);
        }else{
            holder=(ViewHolder) arg1.getTag();
        }

//        if (list.get(position).getTypes().equals("0")) {
//            holder.my_key_type.setText("管理员");
//            holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.guanli));
//
//        } else{
            if (list.get(position).getLock_type()!=null&&list.get(position).getLock_type().equals("6587")){//白马锁
                if (list.get(position).getTypes().equals("0")){
                    holder.my_key_type.setText("管理员");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.guanli));
                }else  if (list.get(position).getTypes().equals("1")){
                    holder.my_key_type.setText("普通用户");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.putong));
                }else  if (list.get(position).getTypes().equals("2")){
                    holder.my_key_type.setText("租户管理员");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.guanli));
                }else{
                    holder.my_key_type.setText("普通用户");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.putong));
                }
            }else{//科技侠
                if (list.get(position).getTypes().equals("0")){
                    holder.my_key_type.setText("管理员");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.guanli));

                    //管理员判断是否已租
                    if (list.get(position).getHas_valid_rent().equals("0")) {
                        holder.hasValidRent_status.setVisibility(View.GONE);
                    } else {
                        holder.hasValidRent_status.setVisibility(View.VISIBLE);
                    }
                }else  if (list.get(position).getTypes().equals("1")){
                    holder.my_key_type.setText("普通用户");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.putong));
                }else  if (list.get(position).getTypes().equals("2")){
                    holder.my_key_type.setText("租户管理员");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.guanli));
                }else{
                    holder.my_key_type.setText("普通用户");
                    holder.personIv.setImageDrawable(context.getResources().getDrawable(R.drawable.putong));
                }
            }

        if (list.get(position).getStartDate()==0&&list.get(position).getEndDate()==0){
            holder.my_key_xianshi.setVisibility(View.GONE);
        }else{
            holder.my_key_xianshi.setVisibility(View.VISIBLE);
        }


//        if (list.get(position).getStatus().equals("110401")){
//            holder.item_my_zhuangtai.setText("正常使用");
//        }else  if (list.get(position).getStatus().equals("110402")){
//            holder.item_my_zhuangtai.setText("分享钥匙，待下载");
//        }else  if (list.get(position).getStatus().equals("110404")){
//            holder.item_my_zhuangtai.setText("冻结中");
//        }else  if (list.get(position).getStatus().equals("110405")){
//            holder.item_my_zhuangtai.setText("已冻结");
//        }else  if (list.get(position).getStatus().equals("110406")){
//            holder.item_my_zhuangtai.setText("解除冻结中");
//        }else  if (list.get(position).getStatus().equals("110407")){
//            holder.item_my_zhuangtai.setText("删除中");
//        }else  if (list.get(position).getStatus().equals("110408")){
//            holder.item_my_zhuangtai.setText("已删除");
//        }else  if (list.get(position).getStatus().equals("110409")){
//            holder.item_my_zhuangtai.setText("重置中");
//        }else  if (list.get(position).getStatus().equals("110410")){
//            holder.item_my_zhuangtai.setText("已重置");
//        }else  if (list.get(position).getStatus().equals("110411")){
//            holder.item_my_zhuangtai.setText("修改中");
//        }
//        if (list.get(position).getIsNew().equals("1")){
//            holder.item_my_zhuangtai.setText("正常使用");
//        }else{
//            holder.item_my_zhuangtai.setText("钥匙过期，请删除");
//        }
        holder.item_my_zhuangtai.setText(list.get(position).getKeyStatus());
        holder.my_key_name.setText(list.get(position).getLockAlias());
        Log.d("keytwoadapter",""+list.get(position).getBattery());
        holder.item_my_dianliang.setText(list.get(position).getBattery()<=0? "" :list.get(position).getBattery()+ "%");
        holder.batteryView.setPower(list.get(position).getBattery());

        return arg1;
    }

    class ViewHolder{
        AutoMarqueeTextView my_key_name;
        TextView  my_key_type,item_my_zhuangtai,item_my_dianliang,my_key_xianshi,hasValidRent_status;
        BatteryView batteryView;
        ImageView personIv;
    }


    //设置数据滚动
}
